#!/bin/bash

directory="/var/named/"
ip="127.0.0.1"
port="53"
zone="cn"
num=10
prefix="test"

confdir="etc"
datadir="data"
logdir="log"
dstdir="."

usage() {
    echo "Usage:"
    echo -e "\t-d directory:\t\tspecify remote bind main directory"
    echo -e "\t-a ip_addr  :\t\tspecify remote bind listened ip address"
    echo -e "\t-p port     :\t\tspecify remote bind listened port"
    echo -e "\t-z zone     :\t\tspecify configured zone name in bind"
    echo -e "\t-n rr_count :\t\tspecify rr count in zone"
    echo -e "\t-f prefix   :\t\tspecify prefix in each rr name"
    echo -e "\t-r dstdir   :\t\tspecify directory store .tgz file"
    echo -e "\t-h          :\t\tprint help info"
}

if [ $# -ne 1 -a $# -ne 14 ]
then
    usage
    exit
fi

args=`getopt -o hdr:a:p:z:n:f: -l directory:,address:,port:,zone:,num:,prefix:,dstdir:,help -- $*`

if [ $? -ne 0 ]
then
    usage
    exit
fi

while getopts hd:a:r:p:z:n:f: OPTION
do
    case $OPTION
    in
        d)directory=$OPTARG;;
        a)ip=$OPTARG;;
        p)port=$OPTARG;;
        z)zone=$OPTARG;;
        n)num=$OPTARG;;
        f)prefix=$OPTARG;;
        r)dstdir=$OPTARG;;
        h)
            usage
            exit;;
        /?)echo "invalid parameters"
           exit;;
    esac
done 

echo "dir:$directory"
echo "ip:$ip"
echo "port:$port"
echo "zone:$zone"
echo "num:$num"
echo "prfix:${prefix}"
count=`expr ${num} - 1`
if [ 'xx'$dstdir == 'xx' ] ; then
   dstdir=`pwd`
fi

#clean
cur_dir=`pwd`
qp_file="${cur_dir}/qp_data"
ab_confdir="${cur_dir}/${confdir}/"
ab_datadir="${cur_dir}/${datadir}/"
ab_logdir="${cur_dir}/${logdir}/"
zone_file="${zone}.txt"
ab_zone_file="${ab_datadir}/${zone_file}"
rm -rf ${confdir} ${datadir} ${logdir} ${qp_file}

#conf file
conf_file="named.conf"
mkdir -p ${confdir}
cd ${confdir}

#option
echo -e "options {" >> ${conf_file} 
echo -e "\tdirectory \"${directory}\";" >> ${conf_file}
echo -e "\tlisten-on port ${port} { ${ip}; };" >> ${conf_file}
echo -e "\tallow-query { any; };" >> ${conf_file}
echo -e "\trecursion no;" >> ${conf_file}
echo -e "};" >> ${conf_file}
echo -e "\n" >> ${conf_file}

#log
echo -e "logging {" >> ${conf_file}
echo -e "\tchannel general_log {" >> ${conf_file}
echo -e "\t\tfile \"${directory}/${logdir}/general_log\" versions 100 size 10m;" >> ${conf_file}
echo -e "\t\tseverity info;" >> ${conf_file}
echo -e "\t\tprint-category yes;" >> ${conf_file}
echo -e "\t\tprint-severity yes;" >> ${conf_file}
echo -e "\t\tprint-time yes;" >> ${conf_file}
echo -e "\t};" >> ${conf_file}
echo -e "\n" >> ${conf_file}

echo -e "\tchannel default_log {" >> ${conf_file}
echo -e "\t\tfile \"${directory}/${logdir}/default_log\" versions 100 size 10m;" >> ${conf_file}
echo -e "\t\tseverity info;" >> ${conf_file}
echo -e "\t\tprint-category yes;" >> ${conf_file}
echo -e "\t\tprint-severity yes;" >> ${conf_file}
echo -e "\t\tprint-time yes;" >> ${conf_file}
echo -e "\t};" >> ${conf_file}
echo -e "\n" >> ${conf_file}

echo -e "\tchannel query_log {" >> ${conf_file}
echo -e "\t\tfile \"${directory}/${logdir}/query_log\" versions 100 size 10m;" >> ${conf_file}
echo -e "\t\tseverity info;" >> ${conf_file}
echo -e "\t\tprint-category no;" >> ${conf_file}
echo -e "\t\tprint-severity no;" >> ${conf_file}
echo -e "\t\tprint-time yes;" >> ${conf_file}
echo -e "\t};" >> ${conf_file}
echo -e "\n" >> ${conf_file}

echo -e "\t#category queries { query_log; };" >> ${conf_file}
echo -e "\tcategory default { default_log; };" >> ${conf_file}
echo -e "\tcategory general { general_log; };" >> ${conf_file}
echo -e "};" >> ${conf_file}
echo -e "\n" >> ${conf_file}

echo -e "zone \"${zone}\" {" >> ${conf_file}
echo -e "\ttype master;" >> ${conf_file}
echo -e "\tfile \"${directory}/${datadir}/${zone_file}\";" >> ${conf_file}
echo -e "};" >> ${conf_file}

#data file
cd ${cur_dir}
mkdir -p ${datadir}
cd ${datadir}
echo "${zone}. 3600   SOA  ns.${zone}. root.${zone}. 12345 1800 900 604800 86400" >> ${ab_zone_file}
echo "${zone}. 3600   NS   ns.${zone}." >> ${ab_zone_file}
echo "ns.${zone}. 3600 A   127.0.0.1">> ${ab_zone_file}
for i in `seq -w 0 ${count}`
do
    echo "${prefix}${i}.${zone}. 3600 A 10.0.0.1" >> ${ab_zone_file}
    echo "${prefix}${i}.${zone}. A" >> ${qp_file}
done

#log file
cd ${cur_dir}
mkdir -p ${logdir}

#compress
cd ${cur_dir}
tar cvfz $dstdir/single_zone.tgz ${confdir} ${datadir} ${logdir}
